package com.demo.amaze.intercepter;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Intercepter implements HandlerInterceptor {

    //在请求处理之前进行调用（Controller方法调用之前）
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("开始进入请求地址拦截");

        String interfaceName = httpServletRequest.getRequestURI();
        String ip = Utils.getRealIP(httpServletRequest);
        long beginTime = System.currentTimeMillis();
        System.out.println("interfaceName为:"+interfaceName);
        System.out.println("ip为:"+ip);
        System.out.println("beginTime为:"+beginTime);
        return true;

    }

    //请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("处理请求完成后视图渲染之前的处理操作（Controller方法调用之后）");
    }

    //在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("视图渲染之后的操作");
    }
}
